package com.vlong.town.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.vlong.town.entity.user.User;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;

/**
 * BaseController
 * @author Assassin
 */
public class BaseController {

    @Autowired
    protected HttpServletRequest request;// HttpServletRequest
    @Autowired
    protected HttpServletResponse response;// HttpServletResponse
    protected int basePageNo = 1;// 分页默认页码
    protected int basePageOffset = 0;// 分页默认记录索引
    protected int basePageSize = 10;// 分页默认一页记录数

    /**
     * 获取分页页码
     * @param pageNo 当前页码
     * @return Integer 页码
     */
    protected Integer getPageNo(Integer pageNo) {
        if(pageNo == null) {
            pageNo = basePageNo;
        }
        return pageNo;
    }

    /**
     * 获取分页记录索引
     * @param pageOffset 当前记录索引
     * @return Integer 页码
     */
    protected Integer getPageOffset(Integer pageOffset) {
        if(pageOffset == null) {
            pageOffset = basePageOffset;
        }
        return pageOffset;
    }

    /**
     * 获取分页一页记录数
     * @param pageSize 记录数
     * @return Integer 记录数
     */
    protected Integer getPageSize(Integer pageSize) {
        if(pageSize == null) {
            pageSize = basePageSize;
        }
        return pageSize;
    }

    /**
     * 验证字符串属性是否合法
     * @param property 字符串属性
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @return true-合法 | false-不合法
     */
    protected boolean validateStringProperty(String property, int minLength, int maxLength) {
        if(!DataUtil.isEmpty(property) && DataUtil.validateLength(property, minLength, maxLength)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取当前登录用户对象
     * @param request HttpServletRequest
     * @return User
     */
    public User getLoginUser(HttpServletRequest request) {
        return (User) request.getSession().getAttribute(TownConstant.TOWN_SESSION_USER);
    }
    
    
    public Long getOrgId() {
		return (Long) request.getSession().getAttribute("orgId");
	}

	public void setOrgId(Long orgId) {
		request.getSession().setAttribute("orgId", orgId);
	}
	
	public Long getUserId() {
		return (Long) request.getSession().getAttribute("userId");
	}

	public void setUserId(Long userId) {
		request.getSession().setAttribute("userId", userId);
	}
	
	public String getRealName() {
		return (String) request.getSession().getAttribute("realName");
	}

	public void setRealName(String realName) {
		request.getSession().setAttribute("realName", realName);
	}
	
	public String getTelphone() {
		return (String) request.getSession().getAttribute("telphone");
	}

	public void setTelphone(String telphone) {
		request.getSession().setAttribute("telphone", telphone);
	}

}
